package ${classPath}.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import ${commonPackage}.controller.BaseController;

import ${classPath}.service.${table.className}Service;

import ${classPath}.entity.${table.className}${entitySuffix};
import ${classPath}.param.${table.className}Param;

/**
 * ${table.remark}Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ${table.className}Controller extends BaseController {

    private Logger logger = LoggerFactory.getLogger(${table.className}Controller.class);

    @Autowired
    private ${table.className}Service ${table.getPropertyName()}Service;

    private final static String LIST_PAGE = "/${table.getPropertyName()}/list";

    private final static String VIEW_PAGE = "/${table.getPropertyName()}/view";

    private final static String UPDATE_PAGE = "/${table.getPropertyName()}/update";
    
    @RequestMapping(value = "/check/${table.getPropertyName()}/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ${table.className}${entitySuffix} ${table.getPropertyName()}${entitySuffix} = new ${table.className}${entitySuffix}();
        resultMap.put("${table.getPropertyName()}${entitySuffix}", ${table.getPropertyName()}${entitySuffix});
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/${table.getPropertyName()}/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ${table.className}${entitySuffix} ${table.getPropertyName()}${entitySuffix} = ${table.getPropertyName()}Service.findById(id);
        resultMap.put("${table.getPropertyName()}${entitySuffix}", ${table.getPropertyName()}${entitySuffix});
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/${table.getPropertyName()}/update",produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(${table.className}${entitySuffix} ${table.getPropertyName()}${entitySuffix}) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer insertCount = ${table.getPropertyName()}Service.update(${table.getPropertyName()}${entitySuffix});
            if (insertCount != null && insertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改${table.remark}成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改${table.remark}失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改${table.remark}出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改${table.remark}出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/check/${table.getPropertyName()}/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = ${table.getPropertyName()}Service.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除${table.remark}成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除${table.remark}失败");
            }
        } catch (Exception e) {
            logger.error("删除${table.remark}出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除${table.remark}出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
       
    @RequestMapping(value = "/uncheck/${table.getPropertyName()}/list")
    public ModelAndView queryList(${table.className}Param ${table.getPropertyName()}Param) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<${table.className}${entitySuffix}> dataList = ${table.getPropertyName()}Service.queryByPage(${table.getPropertyName()}Param);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", ${table.getPropertyName()}Param);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/uncheck/${table.getPropertyName()}/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ${table.className}${entitySuffix} ${table.getPropertyName()}${entitySuffix} = ${table.getPropertyName()}Service.findById(id);
        resultMap.put("${table.getPropertyName()}${entitySuffix}", ${table.getPropertyName()}${entitySuffix});
        return new ModelAndView(VIEW_PAGE, resultMap);
    }
}